package com.javasm.mb_store_api_final.config.adminLogin.login.entity;

import com.javasm.mb_store_api_final.sys_control.rolecontrol.sys_role.entity.SysRole;
import com.javasm.mb_store_api_final.sys_control.usercontrol.authority.entity.Authority;
import com.javasm.mb_store_api_final.sys_control.usercontrol.sys_user.dao.SysUserDao;
import com.javasm.mb_store_api_final.sys_control.usercontrol.sys_user.entity.SysUser;
import com.javasm.mb_store_api_final.sys_control.usercontrol.sys_user.service.SysUserService;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.annotation.Resource;
import java.io.Serializable;
import java.rmi.server.UID;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @className: LoginUser
 * @description
 * @author: gfs
 * @date: 2024/7/25 10:58
 * @version: 0.1
 * @since : jdk11
 */
@NoArgsConstructor
@Getter
public class LoginUser implements UserDetails , Serializable {

    private static final long serialVersionUID = 1L;
    private SysUser adminUser;

    public LoginUser(SysUser adminUser) {
        this.adminUser = adminUser;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //初始化权限标识集合
        List<GrantedAuthority> list = new ArrayList<>();

        String roleName = adminUser.getRoleName();

        SimpleGrantedAuthority authority =new SimpleGrantedAuthority("ROLE_"+roleName);
        list.add(authority);
        //权限集合
        List<Authority> authorities1 = adminUser.getAuthorities();
        for (Authority authority1 : authorities1) {
            list.add(new SimpleGrantedAuthority(authority1.getCode()));
        }

        return list;
    }

    @Override
    public String getPassword() {
        return adminUser.getUserPassword();
    }

    @Override
    public String getUsername() {
        return adminUser.getUserAccount();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return adminUser.getUserState() == 0;
    }
}
